package com.collectplus.express.logic;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.collectplus.express.R;

/**
 * 该类主要封装 app tab的切换效果
 * 
 * @author coffee <br>
 *         2016-1-15下午12:00:55
 */
public class AppTab implements View.OnClickListener {

	private int mPositionTwo;
	private int currentIndex;

	private Activity context;

	private ViewPager mViewPager;
	private TextView[] mTags;
	private View mImgLine;

	private TabChangeCallback callback;

	private int margin = 0;

	public int getMargin() {
		return margin;
	}

	public void setMargin(int margin) {
		this.margin = margin;
	}

	public interface TabChangeCallback {
		public void onTabChanage(int whitch);
	}

	public void setCallback(TabChangeCallback callback) {
		this.callback = callback;
	}

	public AppTab(Activity context) {
		this.context = context;
	}

	public AppTab(Activity context, int margin) {
		this.context = context;
		this.margin = margin;
	}

	/**
	 * 
	 * @param viewPager
	 * @param line
	 * @param tags
	 */
	public void initPosition(ViewPager viewPager, View line, TextView[] tags) {
		this.mViewPager = viewPager;
		this.mImgLine = line;
		this.mTags = tags;

		for (TextView tag : mTags) {
			tag.setOnClickListener(this);
		}
		mViewPager.addOnPageChangeListener(new MyOnPageChangeListener());
		mViewPager.setCurrentItem(0, true);
		mTags[0].setTextColor(context.getResources().getColor(R.color.app_font_1));
		mTags[1].setTextColor(context.getResources().getColor(R.color.app_font_2));
		// 获取屏幕宽度
		DisplayMetrics dm = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mImgLine.getLayoutParams();
		params.leftMargin = margin;
		params.rightMargin = margin;
		// 获取tab宽度
		params.width = screenW / mTags.length - params.leftMargin - params.rightMargin;
		mImgLine.setLayoutParams(params);
		mPositionTwo = screenW / mTags.length;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		// if (id == mTags[0].getId()) {
		// mViewPager.setCurrentItem(0, true);
		// mTags[0].setTextColor(context.getResources().getColor(R.color.app_font_1));
		// mTags[1].setTextColor(context.getResources().getColor(R.color.app_font_2));
		// } else if (id == mTags[1].getId()) {
		// mViewPager.setCurrentItem(1, true);
		// mTags[0].setTextColor(context.getResources().getColor(R.color.app_font_2));
		// mTags[1].setTextColor(context.getResources().getColor(R.color.app_font_1));
		// }
		for (int i = 0; i < mTags.length; i++) {
			if (id == mTags[i].getId()) {
				mViewPager.setCurrentItem(i, true);
			}
		}
	}

	/**
	 * 设置当前选中的tab
	 * 
	 * @param position
	 *            从0开始
	 */
	public void selectTab(int position) {
		onClick(mTags[0]);
	}

	private class MyOnPageChangeListener implements OnPageChangeListener {

		public void onPageSelected(int arg0) {
			Animation animation = null;
			// switch (arg0) {
			// case 0:
			// if (currentIndex == 1) {
			// animation = new TranslateAnimation(mPositionTwo, 0, 0, 0);
			// }
			// mTags[0].setTextColor(context.getResources().getColor(R.color.app_font_1));
			// mTags[1].setTextColor(context.getResources().getColor(R.color.app_font_2));
			// break;
			// case 1:
			// if (currentIndex == 0) {
			// animation = new TranslateAnimation(0, mPositionTwo, 0, 0);
			// }
			// mTags[0].setTextColor(context.getResources().getColor(R.color.app_font_2));
			// mTags[1].setTextColor(context.getResources().getColor(R.color.app_font_1));
			// break;
			// }

			animation = new TranslateAnimation(currentIndex * mPositionTwo, arg0 * mPositionTwo, 0, 0);
			for (int i = 0; i < mTags.length; i++) {
				if (i == arg0) {
					mTags[i].setTextColor(context.getResources().getColor(R.color.app_font_1));
				} else {
					mTags[i].setTextColor(context.getResources().getColor(R.color.app_font_2));
				}
			}
			currentIndex = arg0;
			if (callback != null) {
				callback.onTabChanage(currentIndex);
			}
			animation.setFillAfter(true);
			animation.setDuration(300);
			mImgLine.startAnimation(animation);
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageScrollStateChanged(int arg0) {
		}
	}
}
